﻿using AutoMapper;
using Sheep_Breeding.Domain;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Infeartructure;
using Sheep_Breeding.Write.Api.DTOSheep;
using Sheep_Breeding.Write.Api.OperationServices.IServices;

namespace Sheep_Breeding.Write.Api.OperationServices.Services
{
    public class PhaseBeginServices : IPhaseBeginServices
    {
        private readonly ILogger<PhaseBeginServices> logger;
        private readonly IBaseRepository<PhaseBegin> phaseBeginRepository;
        private readonly IMapper mapper;

        public PhaseBeginServices(ILogger<PhaseBeginServices> logger, IBaseRepository<PhaseBegin> phaseBeginRepository, IMapper mapper)
        {
            this.logger = logger;
            this.phaseBeginRepository = phaseBeginRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> AddPhaseBegin(List<CreatePhaseBeginDTo> createPhaseBeginDToList)
        {
            try
            {
                logger.LogInformation("批量新增期初信息操作");
                var res = new ApiResult();
                var infoList = mapper.Map<List<PhaseBegin>>(createPhaseBeginDToList);
                var model = await phaseBeginRepository.Batchadd(infoList);
                if (model > 0)
                {
                    res.success($"批量期初信息新增成功，共{model}条");
                    return res;
                }
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError("批量期初信息报错" + ex.Message);
                throw ex;
            }
        }
    }
}
